package com.ruoyi.web.controller.dunApplication;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.DunApplication;
import com.ruoyi.domain.DunningFrom;
import com.ruoyi.service.IDunApplicationService;
import com.ruoyi.service.IDunningFromService;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 催款申请Controller
 * 
 * @author ruoyi
 * @date 2020-06-22
 */
@Controller
@RequestMapping("/ruoyi/applications")
public class DunApplicationController extends BaseController
{
    private String prefix = "ruoyi/dunapplication";

    @Autowired
    private IDunApplicationService dunApplicationService;
    @Autowired
    private IDunningFromService dunningFromService;
    @RequiresPermissions("ruoyi:applications:view")
//    @GetMapping("/applications/{pId}")
//    public String application(@PathVariable("pId") Long pId, ModelMap mmap)
//    {
//        mmap.put("pId", pId);
//        return prefix + "/dunapplication";
//    }
    @GetMapping("/applications/{pId}")

    public String application(@PathVariable("pId") Long pId ,ModelMap mmap)
    {
        mmap.put("pId",pId);
        return prefix + "/dunapplication";
    }
    /**
     * 查询催款申请列表
     */
    @RequiresPermissions("ruoyi:applications:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DunApplication dunApplication)
    {
        startPage();
        List<DunApplication> list = dunApplicationService.selectDunApplicationList(dunApplication);
        return getDataTable(list);
    }
    /**
     * 查询催款申请列表
     */
    @RequiresPermissions("ruoyi:applications:list")
    @PostMapping("/lists/{pId}")
    @ResponseBody
    public TableDataInfo lists(@PathVariable("pId") Long pId)
    {
        startPage();
        List<DunningFrom> lists = dunningFromService.selectId(pId);
        for(int i =0;i<lists.size();i++){
            //如果未支付金额为0的时候则把支付状态改为已全部支付
            if(lists.get(i).getaUnpaidAmount().compareTo(BigDecimal.ZERO)==0){
                lists.get(i).setaPaymentStatus(1);
                dunningFromService.updateDunningFrom(lists.get(i));
                //否则支付状态为未完全支付
            }else{
                lists.get(i).setaPaymentStatus(0);
                dunningFromService.updateDunningFrom(lists.get(i));
            }
        }
        return getDataTable(lists);
    }
    /**
     * 导出催款申请列表
     */
    @RequiresPermissions("ruoyi:applications:export")
    @Log(title = "催款申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DunApplication dunApplication)
    {
        List<DunApplication> list = dunApplicationService.selectDunApplicationList(dunApplication);
        ExcelUtil<DunApplication> util = new ExcelUtil<DunApplication>(DunApplication.class);
        return util.exportExcel(list, "applications");
    }

    /**
     * 新增催款申请
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存催款申请
     */
    @RequiresPermissions("ruoyi:applications:add")
    @Log(title = "催款申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DunApplication dunApplication)
    {
        return toAjax(dunApplicationService.insertDunApplication(dunApplication));
    }

    /**
     * 修改催款申请
     */
    @GetMapping("/edit/{dId}")
    public String edit(@PathVariable("dId") Long dId, ModelMap mmap)
    {
        DunningFrom dunningFrom = dunningFromService.selectDunningFromById(dId);
        mmap.put("dunningFrom", dunningFrom);
        return prefix + "/edit";
    }

    /**
     * 修改保存催款申请
     */
    @RequiresPermissions("ruoyi:applications:edit")
    @Log(title = "催款申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DunningFrom dunningFrom)
    {
        DunningFrom dunningFrom2 = dunningFromService.selectDunningFromById(dunningFrom.getdId());
        // 计算总已付余额(原先的已付金额+新加的已付余额)
        //如果已付金额等于null
        if(dunningFrom2.getaAmountPaid()==null&&dunningFrom.getaAmountPaid().compareTo(dunningFrom2.getdCost())<1){
            dunningFrom.getaAmountPaid();
            dunningFrom.setaUnpaidAmount(dunningFrom2.getdCost().subtract(dunningFrom.getaAmountPaid()));
            //如果已付金额不等于null
        }else if(dunningFrom2.getaAmountPaid()!=null){
            if(dunningFrom2.getaAmountPaid().add(dunningFrom.getaAmountPaid()).compareTo(dunningFrom2.getdCost())==1){
                return toAjax(0);
            }else {
               BigDecimal sum=dunningFrom2.getaAmountPaid().add(dunningFrom.getaAmountPaid());
                dunningFrom.setaAmountPaid(sum);
              //  计算未付金额(催款总金额-已付金额)
                if(dunningFrom2.getaUnpaidAmount()!=null) {
                    BigDecimal unsum = dunningFrom2.getdCost().subtract(sum);
                    dunningFrom.setaUnpaidAmount(unsum);
                }else{
                    dunningFrom.setaUnpaidAmount(dunningFrom2.getdCost().subtract(dunningFrom.getaAmountPaid()));
                }
            }
        }else{
            return toAjax(0);
        }

        return toAjax(dunningFromService.updateDunningFrom(dunningFrom));
    }
    /**
     * 删除催款申请
     */
    @RequiresPermissions("ruoyi:applications:remove")
    @Log(title = "催款申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dunApplicationService.deleteDunApplicationByIds(ids));
    }


    //导出所有项目工程款明细
    @Log(title = "项目工程款", businessType = BusinessType.EXPORT)
    @RequiresPermissions("ruoyi:applications:exports")
    @PostMapping("/exports")
    @ResponseBody
    public AjaxResult exports(DunApplication dp)
    {
        List<DunApplication> list = dunApplicationService.selectAllexport(dp);
        ExcelUtil<DunApplication> util = new ExcelUtil<DunApplication>(DunApplication.class);
        return util.exportExcel(list, "工程款明细");
    }
}
